var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();

function floatAdd(t, a) {
    var e, o, n;
    (null == t || null == t || "" == t || isNaN(t)) && (t = 0), (null == a || null == a || "" == a || isNaN(a)) && (a = 0);
    try {
        e = t.toString().split(".")[1].length;
    } catch (t) {
        e = 0;
    }
    try {
        o = a.toString().split(".")[1].length;
    } catch (t) {
        o = 0;
    }
    return (floatMul(t, n = Math.pow(10, Math.max(e, o))) + floatMul(a, n)) / n;
}

function floatMul(t, a) {
    var e = 0, o = t.toString(), n = a.toString();
    try {
        e += o.split(".")[1].length;
    } catch (t) {}
    try {
        e += n.split(".")[1].length;
    } catch (t) {}
    return Number(o.replace(".", "")) * Number(n.replace(".", "")) / Math.pow(10, e);
}

function compareV(t, a) {
    if (t && a) {
        for (var e = t.split("."), o = a.split("."), n = Math.min(e.length, o.length), i = 0, s = 0; i < n && 0 == (s = parseInt(e[i]) - parseInt(o[i])); ) i++;
        return 0 <= (s = 0 != s ? s : e.length - o.length);
    }
    return !1;
}

Page({
    data: {
        videoAd: "",
        hasVideoAd: !1,
        show_sgin: !1,
        show_pop: !1,
        feed_succ: !1,
        feed_fail: !1,
        collect_show: !1,
        subsectionTimeself: "",
        setTimeself: "",
        currencyTimeself: "",
        m_height: 60,
        t_h: "00",
        t_m: "00",
        t_i: "00",
        nh: "00",
        nm: "00",
        ns: "00",
        is_request: 1,
        show_capacity: !1,
        parent_id: 0,
        goldcoin: [],
        is_rule: 1,
        showAddMeBtn: !1,
        surplus: 0,
        show_login: !0,
        is_auth: !0,
        show_bd: !1,
        is_video: 1,
        is_click: 1,
        is_look_video: 1,
        random: 1,
        is_captcha: 1,
        captcha_show: !1,
        captcha_code: "",
        isDouble: !1,
        clicl_ad: 1,
        suspension_style: [ "left:90rpx;top:150rpx", "left:45rpx;top:330rpx", "right:75rpx;top:300rpx", "right:55rpx;top:100rpx" ],
        show_single: !0
    },
    closeCaptcha: function() {
        that.setData({
            captcha_show: !1
        });
    },
    bindCode: function(t) {
        that.setData({
            captcha_code: t.detail.value
        });
    },
    clickSuspension: function(t) {
        var e = t.currentTarget.dataset.index;
        2 == this.data.task[e].type ? wx.navigateToMiniProgram({
            appId: that.data.task[e].appid,
            path: that.data.task[e].path,
            extraData: {},
            envVersion: "release",
            success: function(t) {
                var a = {
                    id: that.data.task[e].id,
                    demo_time: that.data.task[e].number,
                    time: Date.parse(new Date())
                };
                wx.setStorageSync("task", a);
            },
            fail: function(t) {}
        }) : this.NewVideoAdShow(this.data.task[e].id);
    },
    signIn: function() {
        0 < this.data.surplus || parseInt(that.data.today.clock) >= parseInt(that.data.config.clock_num) || (that.data.is_auth ? that.data.is_time_slot ? that.data.config.profit_subscribe_id ? wx.requestSubscribeMessage({
            tmplIds: [ that.data.config.profit_subscribe_id ],
            success: function(t) {
                that.succTips();
            },
            fail: function(t) {},
            complete: function(t) {
                that.conduct();
            }
        }) : that.conduct() : wx.showToast({
            title: "现在不在打卡时间范围内~",
            icon: "none"
        }) : that.setData({
            show_login: !0
        }));
    },
    newSignIn: function() {
        that.data.is_auth ? that.data.config.profit_subscribe_id ? wx.requestSubscribeMessage({
            tmplIds: [ that.data.config.profit_subscribe_id ],
            success: function(t) {
                that.succTips();
            },
            fail: function(t) {},
            complete: function(t) {
                that.newConduct();
            }
        }) : that.newConduct() : that.setData({
            show_login: !0
        });
    },
    random: function() {
        that.setData({
            random: Math.random()
        });
    },
    conduct: function() {
        that.data.hasVideoAd ? this.videoAdShow() : that.verification_completed();
    },
    newConduct: function() {
        that.data.hasVideoAd ? this.videoAdShow() : that.verification_completed();
    },
    NewVideoAdShow: function(a) {
        this.data.videoAd && 1 == that.data.is_video && (this.data.videoAd.show().catch(function() {
            that.data.videoAd.load().then(function() {
                return that.data.videoAd.show();
            }).catch(function(t) {
                wx.showToast({
                    icon: "none",
                    title: "激励视频加载失败~"
                })
            });
        }), that.data.videoAd.offError(function(t) {
        }), that.data.videoAd.onClose(function(t) {
            that.data.videoAd.offClose(), (t && t.isEnded || void 0 === t) && that.receive(a);
        }));
    },
    receive: function(t) {
        var a = {
            action: "receive",
            contr: "task",
            id: t,
            token: wx.getStorageSync("token")
        };
        1 == this.data.is_request && (this.data.is_request = 2, _tools2.default.request(a, function(t) {
            that.loadSuspension(), that.data.is_request = 1, wx.showToast({
                title: "领取奖励成功"
            });
        }));
    },
    succTips: function() {
        var t = {
            action: "addTips",
            contr: "my",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {});
    },
    cancel_login: function() {
        that.setData({
            show_login: !1
        });
    },
    onLoad: function(t) {
        (that = this).data.goldcoin.length = 10, t.parent_id && wx.setStorageSync("parent_id", t.parent_id), 
        t.scene && wx.setStorageSync("parent_id", decodeURIComponent(t.scene)), compareV(wx.getSystemInfoSync().SDKVersion, "2.10.4") || (
        this.setData({
            show_single: !1
        })), that.clearTime();
    },
    adError: function(t) {
        this.setData({
            show_single: !1
        });
    },
    swRule: function() {
        this.setData({
            gdtext: "page-home-frien"
        });
    },
    onReady: function() {
        that.data.is_look_video = 1, that.data.is_click = 1, wx.createSelectorQuery().select("#global-nav").boundingClientRect(function(t) {
            that.setData({
                m_height: t.height
            }), app.globalData.nav_height = t.height;
        }).exec();
        var t = {
            action: "home",
            contr: "index",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info), wx.getStorageSync("showAddMeFlag" + t.info.time) || that.setData({
                showAddMeBtn: !0
            });
            var a = null;
            wx.createInterstitialAd && t.info.config.screen_ad && ((a = wx.createInterstitialAd({
                adUnitId: t.info.config.screen_ad
            })).onLoad(function() {
            }), a.onError(function(t) {
            }), a.onClose(function(t) {
            }), a.show().catch(function(t) {
                console.error(t);
            })), 2 == t.info.config.pattern && (that.loadSuspension(), 2 == t.info.subsection.state && (that.clearTime(), 
            that.subsectionCountdown()));
            var e = wx.getSystemInfoSync();
            t.info.config.video_ad && (compareV(e.SDKVersion, "2.6.0") && (that.data.videoAd = wx.createRewardedVideoAd({
                adUnitId: t.info.config.video_ad
            }), that.data.videoAd.onError(function(t) {
            })), that.setData({
                hasVideoAd: !0
            }));
        }, function(t) {}, function() {}, "", !1);
    },
    subsectionCountdown: function() {
        var t = that.data.subsection.subsection.surplus;
        that.data.subsection.subsection.surplus--;
        var a = Math.floor(t / 60 / 60 % 24), e = Math.floor(t / 60 % 60), o = Math.floor(t % 60);
        if (that.data.surplus < 0) that.clearTime(), that.onReady(); else {
            a < 10 && (a = "0" + a), e < 10 && (e = "0" + e), o < 10 && (o = "0" + o);
            that.data.subsectionTimeself = setTimeout(function() {
                that.setData({
                    subsection: that.data.subsection,
                    nh: a,
                    nm: e,
                    ns: o
                });
                var t = {
                    surplus: that.data.subsection.subsection.surplus,
                    time: Date.parse(new Date())
                };
                wx.setStorageSync("subsectionTimeself", t), that.subsectionCountdown();
            }, 1e3);
        }
    },
    loadSuspension: function() {
        var t = {
            action: "index",
            contr: "task",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    verification_completed: function() {
        2 == that.data.config.is_captcha ? that.setData({
            captcha_show: !0
        }) : that.completed();
    },
    succCaptcha: function() {
        that.data.captcha_code && that.completed();
    },
    completed: function() {
        if (1 == this.data.is_request) {
            this.data.is_request = 2;
            var t = {
                action: "sign",
                contr: "clock",
                token: wx.getStorageSync("token"),
                captcha_code: that.data.captcha_code,
                click_ad: that.data.clicl_ad
            };
            _tools2.default.request(t, function(t) {
                that.data.is_request = 1, that.data.clicl_ad = 1, 2 == that.data.config.is_captcha && (that.setData({
                    captcha_code: ""
                }), that.random()), 2 == t.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: t.info,
                    showCancel: !1
                }) : (that.closeCaptcha(), that.setData(t.info), that.setData({
                    show_bd: !0,
                    bd_img: t.info.bd_img,
                    surplus: that.data.config.clock_interval
                }), 2 != that.data.config.pattern ? (that.loadClock(), 0 < that.data.surplus && (that.clearTime(), 
                that.countdown())) : that.onReady());
            }, function(t) {
                that.data.is_request = 1;
            }, function() {
                that.data.is_request = 1;
            }, "", !0);
        }
    },
    loadClock: function() {
        var t = {
            action: "today",
            contr: "index",
            token: wx.getStorageSync("token")
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
    },
    hideBd: function() {
        that.setData({
            show_bd: !1
        });
    },
    onClickAddToMinProgramCloseBtn: function() {
        wx.setStorageSync("showAddMeFlag" + this.data.time, !0), this.setData({
            showAddMeBtn: !1
        });
    },
    countdown: function() {
        var t = that.data.surplus;
        that.data.surplus--;
        var a = Math.floor(t / 60 / 60 % 24), e = Math.floor(t / 60 % 60), o = Math.floor(t % 60);
        if (that.data.surplus < 0) that.clearTime(); else {
            a < 10 && (a = "0" + a), e < 10 && (e = "0" + e), o < 10 && (o = "0" + o);
            that.data.setTimeself = setTimeout(function() {
                that.setData({
                    surplus: that.data.surplus
                });
                var t = {
                    surplus: that.data.surplus,
                    time: Date.parse(new Date())
                };
                wx.setStorageSync("surplus", t), that.countdown();
            }, 1e3);
        }
    },
    hideSgin: function() {
        that.setData({
            show_sgin: !1
        });
    },
    videoAdShow: function(t) {
        this.data.videoAd && 1 == that.data.is_video && (that.data.is_video = 2, that.data.is_look_video = 2, 
        this.data.videoAd.show().catch(function() {
            that.data.videoAd.load().then(function() {
                return that.data.videoAd.show();
            }).catch(function(t) {
                that.data.is_video = 1, wx.showToast({
                    icon: "none",
                    title: "激励视频加载失败~"
                })
            });
        }), that.data.videoAd.offError(function(t) {
            that.data.is_video = 1
        }), that.data.videoAd.onClose(function(t) {
            that.data.is_video = 1, that.data.videoAd.offClose(), (t && t.isEnded || void 0 === t) && (that.data.config.isChason ? ( 
            2 == that.data.is_click && (that.data.is_look_video = 1, that.data.is_click = 1, 
            that.verification_completed()), that.data.is_look_video = 1, that.data.is_click = 1) : (that.data.clicl_ad = that.data.is_click, 
            that.verification_completed())), that.data.is_look_video = 1, that.data.is_click = 1;
        }));
    },
    onShow: function() {
        var t = wx.getStorageSync("surplus");
        if ((t) && (o = Date.parse(new Date())) - parseInt(t.time) < 1e3 * parseInt(t.surplus)) {
            var a = (1e3 * parseInt(t.surplus) - (o - parseInt(t.time))) / 1e3;
            that.setData({
                surplus: a
            }), that.clearTime(), that.countdown();
        }
        var e = wx.getStorageSync("subsectionTimeself");
        e && ((o = Date.parse(new Date())) - parseInt(e.time) < 1e3 * parseInt(e.surplus) && (that.clearTime(), 
        that.onReady()));
        var o, n = wx.getStorageSync("task");
        n && ((o = Date.parse(new Date())) - n.time > 1e3 * parseInt(n.demo_time) ? that.receive(n.id) : wx.showModal({
            title: "温馨提示",
            content: "需要试玩" + n.demo_time + "秒才能获得奖励哦",
            showCancel: !1,
            success: function(t) {}
        }), wx.removeStorageSync("task"));
        that.loadClock();
    },
    gotoRank: function() {
        wx.navigateTo({
            url: "/bh_rising/pages/rank/rank"
        });
    },
    onHide: function() {
        2 == that.data.is_look_video && that.data.config.isChason && (that.data.is_click = 2);
    },
    onUnload: function() {
        this.clearTime();
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_rising/pages/newIndex/newIndex?parent_id=" + this.data.share.member_id
        };
    },
    clearTime: function() {
        clearTimeout(that.data.setTimeself), clearTimeout(that.data.subsectionTimeself);
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
          desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
            this.getUserInfo(res)
          }
        })
      },
    getUserInfo: function(t) {
        if ("getUserProfile:ok" == t.errMsg) {
            var a = {
                action: "login",
                contr: "my",
                token: wx.getStorageSync("token"),
                encryptedData: t.encryptedData,
                iv: t.iv
            };
            _tools2.default.request(a, function(t) {
                that.setData({
                    is_auth: !0
                });
            });
        }
    },
    closecapacity: function() {
        that.setData({
            show_capacity: !1
        });
    }
});